//------------------------------------------------------------------------------
//  <copyright file="KukaLBR4Tracing.cpp" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
//  </copyright>
//------------------------------------------------------------------------------


#include "stdafx.h"
#include "KukaLBR4Tracing.h"
#include <KukaLbr4Operations.Manifest.h>

namespace KukaLBR4ControllerCommonNS
{
	/// <summary>
	/// Handle for ControllerTimingProvider registration
	/// </summary>
	REGHANDLE controllerTimingRegHandle;

	/// <summary>
	/// Handle for KukaLbr4ControllerProvider registration
	/// </summary>
	REGHANDLE kukaLbr4ControllerRegHandle;

	/// <summary>
	/// Register ETW providers for KUKALBR4Controller
	/// </summary>
	void KukaLBR4Tracing::RegisterProviders()
	{
		EventRegister(&ControllerTimingProvider, NULL, NULL, &controllerTimingRegHandle);
		EventRegister(&KukaLbr4ControllerProvider, NULL, NULL, &kukaLbr4ControllerRegHandle);
	}

	/// <summary>
	/// Unregister ETW providers for KUKALBR4Controller
	/// </summary>
	void KukaLBR4Tracing::UnregisterProviders()
	{
		EventUnregister(controllerTimingRegHandle);
		EventUnregister(kukaLbr4ControllerRegHandle);
	}

	/// <summary>
	/// Write an ETW controller timing event.
	/// This creates an ETW log entry with an integer that indicates the type of event, along with the
	/// time at which the event occurced, and the time since the last event of this type occured.
	/// </summary>
	/// <param name="currentTime"> Time the event occured </param>
	/// <param name="lastTime"> Time of the previous occurence of the same event </param>
	/// <param name="eventDescriptor"> The event descriptor </param>
	/// <param name="eventType"> Integer indicating event type </param>
	void KukaLBR4Tracing::WriteTimingEvent(__int64 currentTime, __int64 lastTime, EVENT_DESCRIPTOR eventDescriptor, int eventType)
	{
		EVENT_DATA_DESCRIPTOR timingEventDataDescriptor[3];

		__int64 freq;

		QueryPerformanceFrequency((LARGE_INTEGER *)&freq);

		double currentTimeInSeconds = (double)currentTime / (double)freq;
		double deltaInSeconds = (double)(currentTime - lastTime) / (double)freq;

		EventDataDescCreate(&timingEventDataDescriptor[0], &eventType, sizeof(int));
		EventDataDescCreate(&timingEventDataDescriptor[1], &currentTimeInSeconds, sizeof(double));
		EventDataDescCreate(&timingEventDataDescriptor[2], &deltaInSeconds, sizeof(double));

		EventWrite(controllerTimingRegHandle,
			&eventDescriptor,
			_ARRAYSIZE(timingEventDataDescriptor),
			timingEventDataDescriptor);
	}

	/// <summary> Write an event to the KukaLBR4ControllerProvider </summary>
	/// <param name="e"> Pointer to event descriptor </param>
	/// <param name="userDataCount"> Length of user data </param>
	/// <param name="userData"> User data </param>
	void KukaLBR4Tracing::WriteKukaLBR4ControllerEvent(PCEVENT_DESCRIPTOR e, ULONG userDataCount, PEVENT_DATA_DESCRIPTOR userData)
	{
		EventWrite(kukaLbr4ControllerRegHandle, e, userDataCount, userData);
	}

	/// <summary> Indicates whether or not an event in the ControllerTimingProvider is enabled </summary>
	/// <param name="e"> Pointer to event descriptor. </param>
	/// <returns> True if event is enabled. </returns>
	bool KukaLBR4Tracing::IsTimingEventsEnabled(PCEVENT_DESCRIPTOR e)
	{
		return (EventEnabled(controllerTimingRegHandle, e) != 0);
	}

	/// <summary> Indicates whether or not an event in the KukaLBR4ControllerProvider is enabled </summary>
	/// <param name="e"> Pointer to event descriptor. </param>
	/// <returns> True if event is enabled. </returns>
	bool KukaLBR4Tracing::IsControllerEventEnabled(PCEVENT_DESCRIPTOR e)
	{
		return (EventEnabled(kukaLbr4ControllerRegHandle, e) != 0);
	}
}

